home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / gimp / 2.0 / help / en / gimp-using-script-fu-tutorial-identifier.html < prev    next >
Encoding:
Extensible Markup Language  |  2008-05-03  |  12.5 KB  |  310 lines

  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4.   <head>
  5.     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  6.     <title>3.2.¬† Variables And Functions</title>
  7.     <link rel="stylesheet" href="gimp-help-plain.css" type="text/css" />
  8.     <link rel="stylesheet" href="gimp-help-screen.css" type="text/css" />
  9.     <link rel="stylesheet" href="gimp-help-custom.css" type="text/css" />
  10.     <link rel="alternate stylesheet" href="gimp22.css" type="text/css" title="gimp22" />
  11.     <meta name="generator" content="DocBook XSL Stylesheets V1.73.2" />
  12.     <link rel="start" href="index.html" title="GNU Image Manipulation Program" />
  13.     <link rel="up" href="gimp-using-script-fu-tutorial.html" title="3.¬† A Script-Fu Tutorial" />
  14.     <link rel="prev" href="gimp-using-script-fu-tutorial.html" title="3.¬† A Script-Fu Tutorial" />
  15.     <link rel="next" href="gimp-using-script-fu-tutorial-lists.html" title="3.3.¬† Lists, Lists And More Lists" />
  16.   </head>
  17.   <body>
  18.     <div class="navheader">
  19.       <table width="100%" summary="Navigation header">
  20.         <tr>
  21.           <th colspan="3" align="center">3.2.¬†
  22.       <span lang="en" xml:lang="en">Variables And Functions</span>
  23.     </th>
  24.         </tr>
  25.         <tr>
  26.           <td width="20%" align="left"><a accesskey="p" href="gimp-using-script-fu-tutorial.html"><img src="../images/prev.png" alt="Prev" /></a>¬†</td>
  27.           <th width="60%" align="center">3.¬†
  28.     <span lang="en" xml:lang="en">A Script-Fu Tutorial</span>
  29.   </th>
  30.           <td width="20%" align="right">¬†<a accesskey="n" href="gimp-using-script-fu-tutorial-lists.html"><img src="../images/next.png" alt="Next" /></a></td>
  31.         </tr>
  32.       </table>
  33.       <hr />
  34.     </div>
  35.     <div class="sect2" lang="en" xml:lang="en">
  36.       <div class="titlepage">
  37.         <div>
  38.           <div>
  39.             <h3 class="title"><a id="gimp-using-script-fu-tutorial-identifier"></a>3.2.¬†
  40.       <span lang="en" xml:lang="en">Variables And Functions</span>
  41.     </h3>
  42.           </div>
  43.         </div>
  44.       </div>
  45.       <p>
  46.       Now that we know that every Scheme statement is enclosed in parentheses,
  47.       and that the function name/operator is listed first, we need to know how
  48.       to create and use variables, and how to create and use functions. We'll
  49.       start with the variables.
  50.     </p>
  51.       <div class="sect3" lang="en" xml:lang="en">
  52.         <div class="titlepage">
  53.           <div>
  54.             <div>
  55.               <h4 class="title"><a id="id2616938"></a>3.2.1.¬†
  56.         <span lang="en" xml:lang="en">Declaring Variables</span>
  57.       </h4>
  58.             </div>
  59.           </div>
  60.         </div>
  61.         <p>
  62.         Although there are a couple of different methods for declaring
  63.         variables, the preferred method is to use the let* construct. If
  64.         you're familiar with other programming languages, this construct is
  65.         equivalent to defining a list of local variables and a scope in which
  66.         they're active. As an example, to declare two variables, a and b,
  67.         initialized to 1 and 2, respectively, you'd write:
  68.       </p>
  69.         <pre class="programlisting">
  70.         (let*
  71.            (
  72.               (a 1)
  73.               (b 2)
  74.            )
  75.            (+ a b)
  76.         )
  77.       </pre>
  78.         <p>or, as one line:</p>
  79.         <pre class="programlisting">(let* ( (a 1) (b 2) ) (+ a b) )</pre>
  80.         <div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
  81.           <table border="0" summary="Note">
  82.             <tr>
  83.               <td rowspan="2" align="center" valign="top" width="25">
  84.                 <img alt="[Note]" src="../images/note.png" />
  85.               </td>
  86.               <th align="left">Note</th>
  87.             </tr>
  88.             <tr>
  89.               <td align="left" valign="top">
  90.                 <p>
  91.           You'll have to put all of this on one line if you're using the
  92.           console window. In general, however, you'll want to adopt a similar
  93.           practice of indentation to help make your scripts more readable.
  94.           We'll talk a bit more about this in the section on White Space.
  95.         </p>
  96.               </td>
  97.             </tr>
  98.           </table>
  99.         </div>
  100.         <p>
  101.         This declares two local variables, a and b, initializes them, then
  102.         prints the sum of the two variables.
  103.       </p>
  104.       </div>
  105.       <div class="sect3" lang="en" xml:lang="en">
  106.         <div class="titlepage">
  107.           <div>
  108.             <div>
  109.               <h4 class="title"><a id="id2617004"></a>3.2.2.¬†
  110.         <span lang="en" xml:lang="en">What Is A Local Variable?</span>
  111.       </h4>
  112.             </div>
  113.           </div>
  114.         </div>
  115.         <p>
  116.         You'll notice that we wrote the summation <code class="code">(+ a b)</code> within
  117.         the parens of the <code class="code">let*</code> expression, not after it.
  118.       </p>
  119.         <p>
  120.         This is because the <code class="code">let*</code>
  121.         statement defines an area in your script in which the declared
  122.         variables are usable; if you type the (+ a b) statement after the
  123.         (let* ...) statement, you'll get an error, because the declared
  124.         variables are only valid within the context of the <code class="code">let*</code>
  125.         statement; they are what programmers call local variables.
  126.       </p>
  127.       </div>
  128.       <div class="sect3" lang="en" xml:lang="en">
  129.         <div class="titlepage">
  130.           <div>
  131.             <div>
  132.               <h4 class="title"><a id="id2617049"></a>3.2.3.¬†
  133.         <span lang="en" xml:lang="en">The General Syntax Of <code class="code">let*</code></span>
  134.       </h4>
  135.             </div>
  136.           </div>
  137.         </div>
  138.         <p>
  139.         The general form of a <code class="code">let*</code> statement is:
  140.       </p>
  141.         <pre class="programlisting">
  142.         (let* ( <em class="replaceable"><code>variables</code></em> )
  143.           <em class="replaceable"><code>expressions</code></em> )
  144.       </pre>
  145.         <p>
  146.         where variables are declared within parens, e.g., (a 2), and
  147.         expressions are any valid Scheme expressions. Remember that the
  148.         variables declared here are only valid within the
  149.         <code class="code">let*</code> statement -- they're local variables.
  150.       </p>
  151.       </div>
  152.       <div class="sect3" lang="en" xml:lang="en">
  153.         <div class="titlepage">
  154.           <div>
  155.             <div>
  156.               <h4 class="title"><a id="id2617100"></a>3.2.4.¬†
  157.         <span lang="en" xml:lang="en">White Space</span>
  158.       </h4>
  159.             </div>
  160.           </div>
  161.         </div>
  162.         <p>
  163.         Previously, we mentioned the fact that you'll probably want to use
  164.         indentation to help clarify and organize your scripts. This is a good
  165.         policy to adopt, and is not a problem in Scheme -- white space is
  166.         ignored by the Scheme interpreter, and can thus be liberally applied
  167.         to help clarify and organize the code within a script. However, if
  168.         you're working in Script-Fu's Console window, you'll have to enter an
  169.         entire expression on one line; that is, everything between the opening
  170.         and closing parens of an expression must come on one line in the
  171.         Script-Fu Console window.
  172.       </p>
  173.       </div>
  174.       <div class="sect3" lang="en" xml:lang="en">
  175.         <div class="titlepage">
  176.           <div>
  177.             <div>
  178.               <h4 class="title"><a id="id2617128"></a>3.2.5.¬†
  179.         <span lang="en" xml:lang="en">Assigning A New Value To A Variable</span>
  180.       </h4>
  181.             </div>
  182.           </div>
  183.         </div>
  184.         <p>
  185.         Once you've initialized a variable, you might need to change its value
  186.         later on in the script. Use the set! statement to change the
  187.         variable's value:
  188.       </p>
  189.         <pre class="programlisting">
  190.         (let* ( (theNum 10) ) (set! theNum (+ theNum theNum)) )
  191.       </pre>
  192.         <p>
  193.         Try to guess what the above statement will do, then go ahead and enter
  194.         it in the Script-Fu Console window.
  195.       </p>
  196.         <div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
  197.           <table border="0" summary="Note">
  198.             <tr>
  199.               <td rowspan="2" align="center" valign="top" width="25">
  200.                 <img alt="[Note]" src="../images/note.png" />
  201.               </td>
  202.               <th align="left">Note</th>
  203.             </tr>
  204.             <tr>
  205.               <td align="left" valign="top">
  206.                 <p>
  207.           The ‚Äú<span class="quote">\</span>‚Äù indicates that there is no line break. Ignore it (don't type
  208.           it in your Script-Fu console and don't hit Enter), just continue
  209.           with the next line.
  210.         </p>
  211.               </td>
  212.             </tr>
  213.           </table>
  214.         </div>
  215.       </div>
  216.       <div class="sect3" lang="en" xml:lang="en">
  217.         <div class="titlepage">
  218.           <div>
  219.             <div>
  220.               <h4 class="title"><a id="id2617180"></a>3.2.6.¬†
  221.         <span lang="en" xml:lang="en">Functions</span>
  222.       </h4>
  223.             </div>
  224.           </div>
  225.         </div>
  226.         <p>
  227.         Now that you've got the hang of variables, let's get to work with some
  228.         functions. You declare a function with the following syntax:
  229.       </p>
  230.         <pre class="programlisting">
  231.         (define
  232.            (
  233.               <em class="replaceable"><code>name</code></em>
  234.               <em class="replaceable"><code>param-list</code></em>
  235.            )
  236.            <em class="replaceable"><code>expressions</code></em>
  237.         )
  238.       </pre>
  239.         <p>
  240.         where <em class="replaceable"><code>name</code></em> is the name assigned to this
  241.         function, <em class="replaceable"><code>param-list</code></em> is a space-delimited
  242.         list of parameter names, and <em class="replaceable"><code>expressions</code></em>
  243.         is a series of expressions that the function executes when it's
  244.         called. For example:
  245.       </p>
  246.         <pre class="programlisting">(define (AddXY inX inY) (+ inX inY) )</pre>
  247.         <p>
  248.         <code class="varname">AddXY</code> is the function's name and
  249.         <code class="varname">inX</code> and <code class="varname">inY</code>
  250.         are the variables. This function takes its two parameters and adds
  251.         them together.
  252.       </p>
  253.         <p>
  254.         If you've programmed in other imperative languages (like C/C++, Java,
  255.         Pascal, etc.), you might notice that a couple of things are absent in
  256.         this function definition when compared to other programming languages.
  257.       </p>
  258.         <div class="itemizedlist">
  259.           <ul type="disc">
  260.             <li>
  261.               <p>
  262.             First, notice that the parameters don't have any "types" (that is,
  263.             we didn't declare them as strings, or integers, etc.). Scheme is a
  264.             type-less language. This is handy and allows for quicker script
  265.             writing.
  266.           </p>
  267.             </li>
  268.             <li>
  269.               <p>
  270.             Second, notice that we don't need to worry about how to "return"
  271.             the result of our function -- the last statement is the value
  272.             "returned" when calling this function. Type the function into the
  273.             console, then try something like:
  274.           </p>
  275.               <pre class="programlisting">(AddXY (AddXY 5 6) 4)</pre>
  276.             </li>
  277.           </ul>
  278.         </div>
  279.       </div>
  280.     </div>
  281.     <div class="navfooter">
  282.       <hr />
  283.       <table width="100%" summary="Navigation footer">
  284.         <tr>
  285.           <td width="40%" align="left"><a accesskey="p" href="gimp-using-script-fu-tutorial.html"><img src="../images/prev.png" alt="Prev" /></a>¬†</td>
  286.           <td width="20%" align="center">
  287.             <a accesskey="u" href="gimp-using-script-fu-tutorial.html">
  288.               <img src="../images/up.png" alt="Up" />
  289.             </a>
  290.           </td>
  291.           <td width="40%" align="right">¬†<a accesskey="n" href="gimp-using-script-fu-tutorial-lists.html"><img src="../images/next.png" alt="Next" /></a></td>
  292.         </tr>
  293.         <tr>
  294.           <td width="40%" align="left" valign="top"><a accesskey="p" href="gimp-using-script-fu-tutorial.html">3.¬†
  295.     <span lang="en" xml:lang="en">A Script-Fu Tutorial</span>
  296.   </a>¬†</td>
  297.           <td width="20%" align="center">
  298.             <a accesskey="h" href="index.html">
  299.               <img src="../images/home.png" alt="Home" />
  300.             </a>
  301.           </td>
  302.           <td width="40%" align="right" valign="top">¬†<a accesskey="n" href="gimp-using-script-fu-tutorial-lists.html">3.3.¬†
  303.       <span lang="en" xml:lang="en">Lists, Lists And More Lists</span>
  304.     </a></td>
  305.         </tr>
  306.       </table>
  307.     </div>
  308.   </body>
  309. </html>
  310.